package manage.action;

import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.html.VueFormResult;
import m.html.form.*;
import m.system.document.DocumentMeta;
import m.system.document.DocumentMethodMeta;
import m.system.document.DocumentParamMeta;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.DictionaryType;
import manage.service.DictionaryTypeService;
import manage.util.DictionaryUtil;

import java.sql.SQLException;

@ActionMeta(name="manage/dictionaryType",title="系统-数据字典")
public class DictionaryTypeAction extends ManageAction {
	
	private DictionaryType model;

	@DocumentMeta(
		method=@DocumentMethodMeta(title="获取数据字典",result = "{code:#{code},map:{\"值\":\"内容|状态|parentOid\"}}"),
		params={
			@DocumentParamMeta(name="model.type",description="数据字典类型",type=FieldType.STRING,length=20,notnull=true,dictType = "manage.model.DictionaryType")
		}
	)
	public JSONMessage getDict(){
		return new JSONMessage(0)
			.push("map", DictionaryUtil.get(model.getType()));
	}
	public JSONMessage getList() throws SQLException, MException {
		return new JSONMessage(0)
			.push("list", ModelQueryList.getModelList(DictionaryType.class,"*"));
	}
	public JSONMessage doSave() throws Exception {
		systemLog("字典","保存字典信息");
		verifyAdminOperPower("manage_system_power");
		getService(DictionaryTypeService.class).save(model);
		return new JSONMessage(0,"保存成功")
			.push("oid",model.getOid());
	}
	public VueResult toEdit() throws Exception {
		verifyAdminOperPower("manage_system_power");
		if(null!=model&&!StringUtil.isSpace(model.getOid())) {
			model=ModelQueryList.getModel(model,"*");
		}else if(null==model){
			model=new DictionaryType();
		}
		ModelForm form = new ModelForm("字典类型",1);
		form.addItem(
			FormInput.input("类型名", "model.name").setPlaceholder("请输入类型名").setRequired(true),
			FormInput.input("类型", "model.type").setPlaceholder("请输入类型").setRequired(true).setDisabled("model.oid?true:false")
		)
		.addData("model",model)
		.addButton(
			FormButton.pri("保存","doSave")
		)
		.addMethod(
			FormEvent.backModel,
			FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
				.success("backModel")
		);
		VueFormResult result=new VueFormResult();
		return result.addForm(form);
	}
	public VueResult toList() throws Exception {
		verifyMenu("A1105");
		return new VuePageResult("page/base/dict/typeList.js");
	}
	public DictionaryType getModel() {
		return model;
	}
	public void setModel(DictionaryType model) {
		this.model = model;
	}

}
